package org.java.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @Author: 马果
 * @Date: 2021/3/19 16:19
 */
public class BaseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转码
        request.setCharacterEncoding("utf-8");
        //获得方法名
        String method = request.getParameter("method");
        //获得字节码对象
        Class clazz = this.getClass();

        try {
            //获得指定方法
            Method handler =clazz.getDeclaredMethod(method,HttpServletRequest.class,HttpServletResponse.class);

            //忽略修饰符
            handler.setAccessible(true);

            //调用方法
            handler.invoke(this,request,response);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
